<template>
  <div>
    <list ref="list">
      <template #toolbar-left>
        <el-button @click="syncReleaseOrderToEs" type="primary" :loading="syncEsLoading">同步到ES</el-button>
        <el-button @click="sendReleaseOrderEmails" type="primary" :loading="sendReleaseOrderEmailLoading">发送放单邮件
        </el-button>
      </template>
    </list>
  </div>
</template>

<script>
import list from "@/components/fam/releaseOrder/list";
import api from "@/api/fam/releaseOrder";
export default {
  components: {
    list
  },
  mounted() {
    this.reload();
  },
  data() {
    return {
      syncEsLoading: false,
      sendReleaseOrderEmailLoading: false
    };
  },
  created() { },
  methods: {
    reload() {
      this.$refs.list.reload();
    },
    syncReleaseOrderToEs() {
      var rows = this.$refs.list.getCheckedRows();
      let message = "确定要将选中的提单同步到ES吗？";
      let postData;
      if (rows && rows.length > 0) {
        postData = { ids: rows.map((item) => item.id) };
      } else {
        message = "确定要将提单列表查询结果同步到ES吗？"
        postData = this.$refs.list.$refs.list.tempQueryParams;
      }
      this.$confirm(message, "提示", {
        type: "warning",
      })
        .then(() => {
          this.syncEsLoading = true;
          console.log(postData);
          api.syncReleaseOrderToEs(postData).then((res) => {
            this.syncEsLoading = false;
          });
        })
        .catch(() => { });
    },
    sendReleaseOrderEmails() {
      var rows = this.$refs.list.$refs.list.getCheckedRowsWithMessage();
      if (!rows) {
        return;
      }
      var data = rows.map(item => ({ billOfLadingId: item.id }));
      this.$confirm("确定要将选中的提单发送放单邮件吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.sendReleaseOrderEmailLoading = true;
          api.sendReleaseOrderEmails(data).then((res) => {
            this.sendReleaseOrderEmailLoading = false;
            if (res.data.length > 0) {
              this.$message.error(`提单${res.data.map(i => i.billOfLadingNo).join(",")}发送失败`);
            }
          });
        })
        .catch(() => { });
    }
  },
};
</script>